package org.aersoft.jokebook.activities;

import java.util.ArrayList;

import org.aersoft.jokebook.R;
import org.aersoft.jokebook.beans.ItemBean;
import org.aersoft.jokebook.core.Constants;
import org.aersoft.jokebook.dao.AppDAO;
import org.aersoft.jokebook.dao.api.IAppDAO;
import org.aersoft.jokebook.fragments.CategoryFragment;

import android.app.Fragment;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

/**
 * List Activity class for to list the items by your respective category.
 * @author Danilo de Godoy Carolino
 */
public class CategoryActivity extends ListActivity {

	private IAppDAO appDAO;
	private DrawerLayout mDrawerLayout;
	private ListView mDrawerList;
	private ActionBarDrawerToggle mDrawerToggle;
	private ArrayList<ItemBean> arrayBeans;
	private int position;
	private int categoryId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_category);

		appDAO = new AppDAO().getContext(this);
		arrayBeans = appDAO.getJokeAllCategories();

		int repeat = 0;
		categoryId = getIntent().getExtras().getInt(Constants.CATEGORY);
		ArrayList<String> titleList = new ArrayList<String>();
		for (ItemBean item : arrayBeans) {
			int listSize = appDAO.getJokeListByCategory(item.getCategoryId()).size();
			titleList.add(item.getCategoryName() + Constants.OPEN_BRACKET + listSize + Constants.CLOSE_BRACKET);
			if (item.getCategoryId() == categoryId)
				position = repeat;
			repeat++;
		}
		appDAO.close();

		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		mDrawerList = (ListView) findViewById(R.id.left_drawer);
		mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
		mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, titleList));
		mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

		getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setHomeButtonEnabled(true);

		mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {
			public void onDrawerClosed(View view) {
				getActionBar().setTitle(getTitle());
				invalidateOptionsMenu();
			}

			public void onDrawerOpened(View drawerView) {
				getActionBar().setTitle(getTitle());
				invalidateOptionsMenu();
			}
		};
		setFragment();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
		menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (mDrawerToggle.onOptionsItemSelected(item)) return true;
		switch (item.getItemId()) {
		case R.id.action_websearch:
			Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
			intent.putExtra(SearchManager.QUERY, getActionBar().getTitle());
			if (intent.resolveActivity(getPackageManager()) != null)
				startActivity(intent);
			else
				Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		mDrawerToggle.syncState();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		mDrawerToggle.onConfigurationChanged(newConfig);
	}

	/**
	 * Get the category ID parameter and send it to other class,
	 * {@link CategoryActivity} Class, like a
	 * parameter, too.
	 * @param id of {@link Integer} type.
	 */
	private void goToCategory(int id) {
		Intent intent = new Intent(this, CategoryActivity.class);
		intent.putExtra(Constants.CATEGORY, id);
		startActivity(intent);
	}

	/**
	 * Set the fragment layout.
	 */
	private void setFragment() {
		Fragment fragment = new CategoryFragment();
		getFragmentManager().beginTransaction().replace(R.id.relativeLayout, fragment).commit();
		mDrawerList.setItemChecked(position, true);
		mDrawerLayout.closeDrawer(mDrawerList);
		mDrawerLayout.setDrawerListener(mDrawerToggle);
	}

	/**
	 * Listener private class for the click event of options bar.
	 */
	private class DrawerItemClickListener implements ListView.OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			mDrawerLayout.closeDrawer(mDrawerList);
			goToCategory(arrayBeans.get(position).getCategoryId());
		}
	}

}